<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.orderSn" clearable class="filter-item" style="width: 200px;" :placeholder="$t('请输入订单编号')"/>
      <el-input v-model="listQuery.subscribeName" clearable class="filter-item" style="width: 200px;" :placeholder="$t('请输入订阅名称')"/>
      <el-select v-model="listQuery.shopId" clearable class="filter-item" :placeholder="$t('请选择门店')">
        <el-option v-for="item in shops" :value="item.id" :label="item.name"></el-option>
      </el-select>
      <el-input v-model="listQuery.goodsName" clearable class="filter-item" style="width: 200px;" :placeholder="$t('请输入商品名称')"/>
      <el-input v-model="listQuery.userName" clearable class="filter-item" style="width: 200px;" :placeholder="$t('请输入用户名称')"/>
      <el-select v-model="listQuery.orderType" clearable class="filter-item" style="width: 200px;"
                 :placeholder="$t('配送方式')">
        <el-option v-for="type in deliveryMethodOptions" :key="type.value" :label="type.label" :value="type.value"/>
      </el-select>

      <el-select v-model="listQuery.status" clearable class="filter-item" style="width: 200px;"
                 :placeholder="$t('发货状态')">
        <el-option v-for="type in statusOptions" :key="type.value" :label="type.label" :value="type.value"/>
      </el-select>

      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter" v-permission="['GET /admin/p/lumiere_subscribe_order_index']">{{$t('Search')}}</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" :element-loading-text="$t('Searching')" border fit highlight-current-row>

      <el-table-column align="center" :label="$t('订单编号')" prop="orderSn">
      </el-table-column>

      <el-table-column align="center" :label="$t('门店名称')" prop="shopName">

      </el-table-column>

      <el-table-column align="center" :label="$t('订阅名称')" prop="subscribeName">
      </el-table-column>

      <el-table-column align="center" :label="$t('商品')" prop="goodsNames">
        <template slot-scope="scope">
          <el-row v-for="goods in scope.row.goods">
            ${{goods.goodsName}}
          </el-row>
        </template>
      </el-table-column>
      <!--订单类型(1:自提；2：外送;3:堂食；4：邮寄)-->
      <el-table-column align="center" :label="$t('价值')" prop="orderPrice">
      </el-table-column>

      <el-table-column align="center" :label="$t('订单时间')" prop="addTime">
      </el-table-column>

      <el-table-column align="center" :label="$t('配送方式')" prop="orderType">
        <template slot-scope="scope">
          {{filterTransportType(scope.row.orderType)}}
        </template>
      </el-table-column>

      <el-table-column align="center" :label="$t('配送/取货时间')"   prop="receiptRange">
      </el-table-column>

      <el-table-column align="center" :label="$t('用户名称')"   prop="userName">
      </el-table-column>

      <el-table-column align="center" :label="$t('状态')"   prop="orderStatus">
        <template slot-scope="scope">
          {{filterShipStatus(scope.row.orderStatus)}}
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('Operate')" width="400" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="info" icon="el-icon-view" plain1 size="mini" v-permission="['GET /admin/p/lumiere_subscribe_order_detail']" @click="handleDetail(scope.row)">{{$t('Details')}}</el-button>
        </template>
      </el-table-column>
    </el-table>


    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-tooltip placement="top" :content="$t('Back_to_top')">
      <back-to-top :visibility-height="100" />
    </el-tooltip>
  </div>
</template>

<script>
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination'
import { querySubscribeOrder } from '../../api/subscribeOrder'
import { allForPerm } from '../../api/shop' // Secondary package based on el-pagination
export default {
  name: "subscribeOrder",
  components: { BackToTop, Pagination },
  data(){

    const defaultDeliveryMethodOptions = [
      {
        label: this.$t('自提'),
        value: 1
      },
      {
        label: this.$t('配送'),
        value: 2
      },
      {
        label: this.$t('堂食'),
        value: 3
      },
      {
        label: this.$t('外带'),
        value: 4
      },
    ]


    const defaultStatusOptions = [
      {
        label: this.$t('Unpaid1'),
        value: 101
      },
      {
        label: this.$t('Member_cancelled'),
        value: 102
      },
      {
        label: this.$t('System_cancelled'),
        value: 103
      },
      {
        label: this.$t('Paid'),
        value: 201
      },
      {
        label: this.$t('Apply_for_refund'),
        value: 202
      },
      {
        label: this.$t('Refunded'),
        value: 203
      },
      {
        label: this.$t('Sent_for_delivery'),
        value: 301
      },
      {
        label: this.$t('Member_received'),
        value: 401
      },
      {
        label: this.$t('System_received'),
        value: 402
      }
    ]

    return {
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        subscribeName: undefined,
        userAccount: undefined,
        shopId: undefined,
        orderSn: undefined,
        page: 1,
        limit: 20,
        name: undefined,
        sort: 'add_time',
        order: 'desc'
      },
      goodsOps: [],
      shops: [],
      deliveryMethodOptions: Object.assign({}, defaultDeliveryMethodOptions),
      statusOptions: Object.assign({}, defaultStatusOptions),
    }
  },
  created() {
    this.getList()
    allForPerm().then(response=>{
      this.shops = response.data.data.list
    })
  },
  methods: {
    getList() {
      this.listLoading = true
      querySubscribeOrder(this.listQuery).then(response => {
        // //debugger
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    filterShipStatus(orderStatus){
      if(orderStatus == 101){
        return this.$t('Unpaid')
      }else if(orderStatus == 102){
        return this.$t('Member_cancelled')
      }else if(orderStatus == 103){
        return this.$t('System_cancelled')
      }else if(orderStatus == 201){
        return this.$t('Paid')
      }else if(orderStatus == 202){
        return this.$t('Apply_for_refund')
      }else if(orderStatus == 203){
        return this.$t('Refunded')
      }else if(orderStatus == 301){
        return this.$t('Sent_for_delivery')
      }else if(orderStatus == 401){
        return this.$t('Member_received')
      }else if(orderStatus == 402){
        return this.$t('System_received')
      }
    },
    filterTransportType(transportType){
      if(transportType == 1){
        return this.$t('自取')
      }else if (transportType == 2) {
        return this.$t('门店配送')
      }else{
        return this.$t('物流')
      }
    },
    handleDetail(row){
      this.$router.push({ path: '/subscribe/subscribeDetail', query: { id: row.id }})
    }
  }
}
</script>

<style scoped>

</style>
